document.querySelector('.img__btn').addEventListener('click', function() {
    document.querySelector('.content').classList.toggle('s--signup')
})

//校验手机号的正则表达式
let phoneRegex = /^1[3|4|5|6|7|8|9][0-9]{9}$/;

//登录
function login() {
    let data = {};
    let phone = $("#login_phone").val();
    let password = $("#login_password").val();

    //校验数据
    if(checkNull(phone)) {
        $.messager.alert("提示", "手机号不能为空!", "info");
        return;
    }
    if(checkNull(password)) {
        $.messager.alert("提示", "密码不能为空!", "info");
        return;
    }
    if(!phoneRegex.test(phone)) {
        $.messager.alert("提示", "请输入正确格式的手机号!", "info");
        return;
    }
    data.phone = phone;
    data.password = password;
    //进行登录
    $.ajax({
        url: "user_login",
        type:'post',
        contentType : "application/json",
        data: JSON.stringify(data),
        dataType:'json',
        success:function(value){
            if(value.success) {
                //将令牌存入cookie
                $.cookie("token", value.data, {expires: 30});
                //转到用户信息页面
                location.href = "userInfo.html";
            } else {
                $.messager.alert("提示" ,value.data, "info");
            }
        }
    });
}

//注册
function regist() {
    //获取表单数据
    let data = {};
    let phone = $("#regist_phone").val();
    let password = $("#regist_password").val();
    let repassword = $("#regist_repassword").val();

    //校验数据
    if(checkNull(phone)) {
        $.messager.alert("提示", "手机号不能为空!", "info");
        return;
    }
    if(checkNull(password)) {
        $.messager.alert("提示", "密码不能为空!", "info");
        return;
    }
    if(password.length < 8) {
        $.messager.alert("提示", "密码不能少于8位!", "info");
        return;
    }
    if(checkNull(repassword)) {
        $.messager.alert("提示", "确认密码不能为空!", "info");
        return;
    }
    if(password != repassword) {
        $.messager.alert("提示", "两次密码输入不一致!", "info");
        return;
    }
    if(!phoneRegex.test(phone)) {
        $.messager.alert("提示", "请输入正确格式的手机号!", "info");
        return;
    }

    data.phone = phone;
    data.password = password;

    //进行注册
    $.ajax({
        url: "user_regist",
        type:'post',
        contentType : "application/json",
        data: JSON.stringify(data),
        dataType:'json',
        success:function(value){
            if(value.success) {
                /*//将令牌存入cookie
                $.cookie("token", value.data, {expires: 30});
                //转到用户信息页面
                location.href = "userInfo.html";*/
                $.messager.alert("提示" , "注册成功，可以进行登录了!", "info");
            } else {
                $.messager.alert("提示" ,value.data, "info");
            }
        }
    });
}

//判断字符串是否为空
function checkNull(value) {
    if (value == null || value == "") {
        return true;
    } else {
        return false;
    }
}
